
//
// Created by hay boy on 2022/7/3.
//

#ifndef ALGORITHMS_OFFLINEEQUIVALENCECLASS_H
#define ALGORITHMS_OFFLINEEQUIVALENCECLASS_H
#include "../stack/ArrayStack.h"
void offlineEquivalenceClass(){
    int n,r;
    cout<<"Enter number of elements"<<endl;
    cin>>n;
    if(n<2){
        cout<<"Too few elements"<<endl;
        return;
    }
    cout<<"Enter number of the relations"<<endl;
    cin>>r;
    if(r<1){
        cout<<"Too few relations"<<endl;
        return;
    }
    auto* list = new ArrayStack<int>[n+1];
    int a,b;
    for(int i = 1;i<=r;i++){
        cout<<"Enter next relation/pair"<<endl;
        cin>>a>>b;
        list[a].push(b);
        list[b].push(a);
    }
    ArrayStack<int> unprocessedList;
    bool *out = new bool[n+1];
    for(int i = 1;i<=n;i++){
        out[i] = false;
    }
    for(int i = 1;i<=n;i++){
        if(!out[i]){
            cout<<"Next class is: "<<i<<" ";
            out[i] = true;
            unprocessedList.push(i);
            while(!unprocessedList.empty()){
                int j = unprocessedList.top();
                unprocessedList.pop();
                while(!list[j].empty()){
                    int q = list[j].top();
                    list[j].pop();
                    if(!out[q]){
                        cout<<q<<" ";
                        out[q] = true;
                        unprocessedList.push(q);
                    }

                }
                cout<<endl;
            }
        }
    }
    cout<<"End of list of equivalence classes"<<endl;
}
#endif //ALGORITHMS_OFFLINEEQUIVALENCECLASS_H
